﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using NetDiskPRJ.Common.DEncrypt;
using DevExpress.XtraEditors;

namespace NetDiskPRJ.CntCfg
{
    /// <summary>
    /// 数据库连接参数设置
    /// </summary>
    public partial class CntCfg : DevExpress.XtraEditors.XtraForm
    {
        public CntCfg()
        {
            InitializeComponent();
        }

        private void CntCfg_Load(object sender, EventArgs e)
        {
            try
            {
                string _connectionString = "";
                string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "NetDiskPRJ.exe.config";
                XmlDocument doc = new XmlDocument();
                doc.Load(strFileName);
                XmlNodeList nodes = doc.GetElementsByTagName("appSettings");
                for (int i = 0; i < nodes.Count; i++)
                {
                    XmlNodeList cnodes = nodes[i].ChildNodes;
                    for (int j = 0; j < cnodes.Count; j++)
                    {

                        XmlAttributeCollection _xmlatt = cnodes[j].Attributes;

                        if (_xmlatt["key"].Value == "ConnectionString")
                        {
                            _connectionString = _xmlatt["value"].Value;
                            break;
                        }
                    }
                }

                string connstr = DESEncrypt.Decrypt(_connectionString);
                string[] strarr = connstr.Split(';');
                if (strarr.Count() > 4)
                {
                    txtServer.Text = strarr[0].Replace("server=", "");
                    txtDataBase.Text = strarr[1].Replace("database=", "");
                    txtLoginID.Text = strarr[2].Replace("uid=", "");
                    txtPassword.Text = strarr[3].Replace("pwd=", "");
                }
            }
            catch
            {
                XtraMessageBox.Show("配置文件读取错误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string strconfig = "server={0};database={1};uid={2};pwd={3};";
                object[] objarr = new object[4];
                objarr[0] = txtServer.Text;
                objarr[1] = txtDataBase.Text;
                objarr[2] = txtLoginID.Text;
                objarr[3] = txtPassword.Text;
                string Encryptstr = DESEncrypt.Encrypt(String.Format(strconfig, objarr));
                Common.ConfigHelper.SeTconfigKeyValue("ConnectionString", Encryptstr);

                XtraMessageBox.Show("配置成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch
            {
                XtraMessageBox.Show("配置错误，请联系管理员！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
